package com.lam.framework.aop;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.lam.common.components.dict.DictUtil;
import com.lam.common.components.dict.annotation.DictAutoTransfer;
import com.lam.common.components.dict.model.DictFeature;
import com.lam.common.utils.DateUtil;

/**
 * 字典aop类
 */
@Order(10)
@Aspect
@Component
public class DictAspect {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Pointcut("execution(public * com.lam..*.*Controller.*(..)) && @annotation(com.lam.common.components.dict.annotation.DictAutoTransfer)")
	public void excudeService() {
	}

	@Around("excudeService()")
	public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
		Object result = joinPoint.proceed();
		DictAutoTransfer dictAutoTransfer = ((MethodSignature)joinPoint.getSignature()).getMethod().getAnnotation(DictAutoTransfer.class);
		if (dictAutoTransfer.value() && DictUtil.supportDataType(result)) {
			long start = System.currentTimeMillis();
			result = DictUtil.convertJavaObject(result, DictFeature.create().transfer2Json(true).deep(dictAutoTransfer.deep()));
			long end = System.currentTimeMillis();
			logger.info("翻译字典数据完成，耗时:{}", DateUtil.convertTimeLong(end - start));
		}
		return result;
	}
	
}
